package com.rui.study.micro.attach.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletResponse;
import java.io.*;
import java.util.Map;

@Slf4j
@RefreshScope
@RestController
public class AttachController {

    public static final String PATH = "D:\\temp";
    public static final int _1MB = 1024 * 1024;

//    @GetMapping(path = "download")
//    public void getInputStream(ServletResponse response, String filename) {
//        log.info("filename: " + filename);
//        InputStream in = null;
//        OutputStream out = null;
//        try {
//            in = new FileInputStream(new File(PATH, filename));
//            out = response.getOutputStream();
////            byte[] buf = new byte[_1MB];
//            byte[] buf = new byte[_1MB];
//            int len;
//            while ((len = in.read(buf)) > 0) {
//                out.write(buf, 0, len);
//            }
//            out.flush();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } finally {
//            try {
//                in.close();
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            } finally {
//                try {
//                    out.close();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
//    }

    @GetMapping(path = "download")
    public ResponseEntity getInputStream(String filename) {
        log.info("filename: {}", filename);
        InputStream in = null;
        byte[] buf;
        try {
            in = new FileInputStream(new File(PATH, filename));
            buf = new byte[in.available()];
            log.debug("file size is: {} bytes", buf.length);
            in.read(buf);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + filename);

        return new ResponseEntity(buf, httpHeaders, HttpStatus.OK);
    }

//    @GetMapping(path = "download")
//    public InputStream getInputStream(String filename) {
//        log.info("filename: {}", filename);
//        InputStream in;
//        try {
//            in = new FileInputStream(new File(PATH, filename));
//        } catch (FileNotFoundException e) {
//            throw new RuntimeException(e);
//        }
//        return in;
//    }

    @GetMapping("hello")
    public String sayHello() {
        return "hello";
    }

    @PostMapping("hello")
    public String sayHello(@RequestBody Map<String, Object> param) {
        return "hello " + param.get("name");
    }
}
